const dataBehavior = Behavior({
  // lifetimes:{ // 保存情况之一
  //   detached() {
  //     if(this.data.group_count > 0 || this.data.my_count > 0) { 
  //       this.setData({infer_log: 'here before [saveRecord]'})
  //       this.saveRecord()
  //       this.setData({infer_log: 'here before [clearData]'})
  //       this.clearData()
  //     }
  //     else {}
  //     console.log("动作计数detached")
  //   }
  // },
  methods: {
    // mode: 0 squat, 1 push-up
    saveRecord(mode) {
        const date_now = new Date().toLocaleDateString()
        const time_now = new Date().toTimeString().substring(0,8)

        let usr_id = getApp().globalData.userID
        if (mode == 0) {
          wx.cloud.database().collection('squat-count').add({
            data: {
              id: usr_id,
              date: date_now,
              when: time_now,
              groupNum: this.data.group_count, // 组数
              resNum: this.data.my_count // 未满一组的计数
            },
            success(res){
              console.log("数据保存成功", res)
              wx.showToast({
                title: '数据已保存',
                icon: 'success',
                duration: 2000
              })
            },
            fail(res){
              console.log("数据保存失败", res)
              wx.showToast({
                title: '数据保存失败',
                icon: 'error',
                duration: 2000
              })
            }
          })
        }
        else if (mode == 1) {
          wx.cloud.database().collection('pushup-count').add({
            data: {
              id: usr_id,
              date: date_now,
              when: time_now,
              groupNum: this.data.group_count, // 组数
              resNum: this.data.my_count // 未满一组的计数
            },
            success(res){
              console.log("数据保存成功", res)
              wx.showToast({
                title: '数据已保存',
                icon: 'success',
                duration: 2000
              })
            },
            fail(res){
              console.log("数据保存失败", res)
              wx.showToast({
                title: '数据保存失败',
                icon: 'error',
                duration: 2000
              })
            }
          })
        }
        
    },
    clearData() {
      // recogBehavior
      this.setData({
        infer_log: null,

        left_elbow: null,
        right_elbow: null,
        left_leg: null,
        right_leg: null,
        head: null,
  
        left_elbow_angle_log: 0.0,
        right_elbow_angle_log: 0.0,
        left_leg_angle_log: 0.0,
        right_leg_angle_log: 0.0,
        leaning_angle_log: 0.0,
    
        left_elbow_angle: 0.0,
        right_elbow_angle: 0.0,
        left_leg_angle: 0.0,
        right_leg_angle: 0.0,
        leaning_angle: 0.0,

        origin: null,
        size: null, // w, h
      })
      // countBehavior
      this.setData({
        last_pose: 0, 
        squat_flag: false,
    
        max_count: 10,
        my_count: 0,
        last_count: 0,
        timer: null,
    
        group_count: 0,
        percentage: 0,
      })
    },
    usrSaveAction(e) {
      if(this.data.group_count > 0 || this.data.my_count > 0) { 
        this.setData({infer_log: 'here before [saveRecord]'})
        var mode = e.currentTarget.dataset.mode
        this.saveRecord(mode) // add mode here
        this.setData({infer_log: 'here before [clearData]'})
        this.clearData()

        //return true 
      }
      else {
        this.nothingToSave()
      }
      this.setData({infer_log: 'here end of [usrSaveAction]'})
    },
    nothingToSave() {
      wx.showToast({
        title: '暂无可保存数据',
        icon: 'none',
        duration: 2000
      })
    }
  }
})

export default dataBehavior

